package com.gin.no_gay_alliance.adpter.history;

import android.content.Context;
import android.content.DialogInterface;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.activity.ReplyHistoryTopicActivity;
import com.gin.no_gay_alliance.activity.TopicReadActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.entity.ReplyHistoryTopic;
import com.gin.no_gay_alliance.database.manager.ReplyHistoryManager;
import com.gin.no_gay_alliance.databinding.ItemObservedTopicBinding;
import com.gin.no_gay_alliance.listener.PopupOnMenuItemClickListener;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.PopupMenuUtils;
import com.gin.no_gay_alliance.utils.ViewUtils;

import java.util.List;
import java.util.Locale;

/**
 * 观察主题适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/16 17:48
 **/
public class ObservedTopicAdapter extends BaseBindingRecyclerViewAdapter<ItemObservedTopicBinding, ReplyHistoryTopic> implements View.OnClickListener {
    private PopupOnMenuItemClickListener<ReplyHistoryTopic> listener;

    public ObservedTopicAdapter(Context context) {
        super(context);
    }

    public ObservedTopicAdapter(@NonNull Context context, @NonNull List<ReplyHistoryTopic> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemObservedTopicBinding> holder, @NonNull ItemObservedTopicBinding b, int position, ReplyHistoryTopic item) {
        b.tvTopicTitle.setTag(item);
        b.tvTopicTitle.setText(item.title);

        int count = MyApplication.app.ngaDatabase.replyHistoryDao().count(item.topicId);
        b.tvCount.setText(String.format(Locale.CHINA, "记录回复: %d 个", count));


        View[] views = {b.ivMore, b.getRoot()};
        ViewUtils.setOnClickListener(this, views);
        ViewUtils.setTag(item, views);


        if (this.listener == null) {
            this.listener = new PopupOnMenuItemClickListener<ReplyHistoryTopic>() {
                @Override
                protected boolean onMenuItemClick(ReplyHistoryTopic item, MenuItem menuItem, int itemId) {
                    int p = data.indexOf(item);
                    // 打开原帖
                    if (itemId == R.id.menu_item_open) {
                        TopicReadActivity.startActivity(context, item.title, item.topicId);
                    }
                    // 移除观察
                    if (itemId == R.id.menu_item_remove_observed_topic) {
                        AlertDialogUtils.showConfirm(context, "移出观察", "将本帖移出观察列表, 在'常规模式'下不再记录其中的回复内容", (d, which) -> {
                            if (which == DialogInterface.BUTTON_POSITIVE) {
                                ReplyHistoryManager.removeObservedTopic(item.topicId);
                                data.remove(p);
                                notifyItemRemoved(p);
                            }
                        });
                    }
                    // 清空记录
                    if (itemId == R.id.menu_item_clear) {
                        AlertDialogUtils.showConfirm(context, "清空记录", "将清空已记录的本帖回复内容", (d, which) -> {
                            if (which == DialogInterface.BUTTON_POSITIVE) {
                                MyApplication.app.ngaDatabase.replyHistoryDao().clear(item.topicId);
                                notifyItemChanged(p);
                            }
                        });
                    }
                    return true;
                }
            };
        }
        this.listener.setItem(item);

    }

    @Override
    public void onClick(View v) {
        ReplyHistoryTopic item = (ReplyHistoryTopic) v.getTag();

        // 打开菜单选择其他操作： 清空记录，移除观察，打开原帖
        if (v.getId() == R.id.iv_more) {
            PopupMenuUtils.create(context, v, R.menu.observed_topic_menu, this.listener).show();
        }

        // 点击标题 打开主题
        if (v.getId() == R.id.layout_parent) {
            ReplyHistoryTopicActivity.startActivity(context, item.topicId, item.title);
        }
    }
}
